<!--
 * @Descripttion: 活动分类
 * @Author: xiao li
 * @Date: 2020-10-15 10:24:27
 * @LastEditors: xiao li
 * @LastEditTime: 2020-12-08 14:45:49
-->
<template>
  <div>
    <top-nav></top-nav>
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          @click="addEditActivifyClassify"
          icon="el-icon-plus"
          >添加活动分类</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="title" label="分类名"> </el-table-column>
        <el-table-column prop="top" label="排序值"> </el-table-column>
        <el-table-column prop="status" label="是否启用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateItem(scope.row.id, scope.row.status)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="addEditActivifyClassify(scope.row)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="page"
        :pageSize="limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
      <el-dialog
        :title="dialogForm.id ? '编辑分类' : '新增分类'"
        :visible.sync="showDialogForm"
        width="480px"
        center
      >
        <el-form
          @submit.native.prevent
          :model="dialogForm"
          :rules="dialogFormRules"
          ref="dialogForm"
          label-width="80px"
        >
          <el-form-item label="分类名" prop="title">
            <el-input
              v-model="dialogForm.title"
              placeholder="请输入分类名"
            ></el-input>
          </el-form-item>
          <el-form-item label="排序值" prop="top">
            <el-input
              v-model.number="dialogForm.top"
              placeholder="请输入排序值"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialogForm = false">{{
            $t('action.cancel')
          }}</el-button>
          <el-button type="primary" @click="submitFormInfo('dialogForm')">{{
            $t('action.comfirm')
          }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      tableData: [],
      searchForm: {
        page: 1,
        limit: 10
      },
      total: 0,
      showDialogForm: false,
      dialogForm: {
        id: '',
        title: '',
        top: 0
      },
      dialogFormRules: {
        title: { required: true, type: 'string', message: '请输入分类名', trigger: 'blur' },
        top: { required: true, type: 'number', message: '请输入排序值', trigger: 'blur' }
      }
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      this.$api.overlordCateList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    submitFormInfo (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          let { dialogForm } = this
          dialogForm.title = JSON.parse(JSON.stringify(dialogForm.title)).replace(/^\s+|\s+$/g, '')
          if (!dialogForm.title) {
            this.$message.error('请输入分类名！')
            return
          }
          let methodModel = dialogForm.id ? 'overlordCateUpdate' : 'overlordCateAdd'
          this.$api[methodModel](dialogForm).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t(dialogForm.id ? 'tips.successRev' : 'tips.successSave'))
              this.showDialogForm = false
              this.getTableDataList()
            }
          })
        }
      })
    },
    addEditActivifyClassify (item = {}) {
      if (!item.top) {
        item.top = 0
      }
      for (let key in this.dialogForm) {
        this.dialogForm[key] = item[key]
      }
      this.showDialogForm = !this.showDialogForm
    },
    /**
     * @method: 上下架/删除
     * @param {*} id
     * @param {*} status
     */
    async updateItem (id, status) {
      let { code } = await this.$api.overlordCateUpdate({ id, status })
      if (code === 200) {
        this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
        if (status === -1) {
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        }
      } else {
        if (status === -1) return
        this.getTableDataList()
      }
    },
    // 删除
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input {
  width: 300px;
}
</style>
